<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

    <!-- Bootstrap -->
    <link href="<?=base_url();?>public/resource/default/vendor/bootstrap/css/bootstrap.css" rel="stylesheet">
    <link href="<?=base_url();?>public/resource/default/vendor/datetimepicker/bootstrap-datetimepicker.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link href="<?=base_url();?>public/resource/default/vendor/icheck/skins/minimal/_all.css" rel="stylesheet" >
    <link href="<?=base_url();?>public/resource/default/vendor/select2/select2.css" rel="stylesheet" >
    <link href="<?=base_url();?>public/resource/default/vendor/bs3editable/css/bootstrap-editable.css" rel="stylesheet" >

    <link href="<?=base_url();?>public/resource/default/css/style.css" rel="stylesheet" >

</head>
<body>

<div class="container-fluid wp-content">

    <div class="wp-content-mbx">
        <a><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span></a>
        <div>
            <span class="mainhead">编辑文章</span>
            <span class="subhead">这里是其他的说明性文字</span>
        </div>
    </div>

    <div class="row">
        <form id="new-post-fm">
            <input type="hidden" name="id" value="<?=hashids_encode($post->ID)?>" >
            <div class="col-sm-8 col-md-8">
                <div class="wp-panel panel-default">
                    <div class="wp-panel-body">

                        <div class="form-group">
                            <input class="form-control post-title" type="text" name="postTitle" value="<?=$post->post_title?>" placeholder="此处输入文章标题">
                        </div>
                        <div class="form-group" style="height: 390px;">
                            <div id="editor" ><?=$post->post_content?></div>
                            <input type="hidden" name="postContent">
                        </div>

                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-heading">文章摘要</div>
                    <div class="panel-body">
                        <textarea class="form-control" name="postExcerpt"><?=$post->post_excerpt?></textarea>
                    <span class="help-block">
                        摘要是可选的手工创建的内容总结，用于简单的概括文章内容
                    </span>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-heading">别名</div>
                    <div class="panel-body">
                        <input class="form-control wd2" type="text" name="postName" value="<?=$post->post_name?>" />
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-heading">作者</div>
                    <div class="panel-body">
                        <select class="form-control wd2" name="postAuthor">
                            <option value="">作为顶级菜单</option>
                        </select>
                    </div>
                </div>

            </div>

            <div class="col-sm-4 col-md-4">
                <div class="wp-panel panel-default">
                    <div class="panel panel-default">
                        <div class="panel-heading">发布</div>

                        <div class="panel-body">
                            <ul id="post-base-info" class="list-unstyled">
                                <li>
                                    <label class="icheck-text"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>&nbsp;状态</label>
                                    <a href="#" id="post-status" data-type="select" data-pk="1" data-value="<?=$post->post_status?>" data-title="选择文章状态"></a>
                                    <input type="hidden" name="postStatus" />
                                </li>
                                <li>
                                    <label class="icheck-text"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>&nbsp;公开度</label>
                                    <a href="#" id="ping-status" data-type="select" data-pk="1" data-value="<?=$post->ping_status?>" data-title="选择文章公开度"></a>
                                    <input type="hidden" name="pingStatus" /> <br>
                                    <input class="hidden" type="text" name="postPassword" placeholder="输入查看密码" />
                                </li>
                                <li>
                                    <label class="icheck-text"><span class="glyphicon glyphicon-time" aria-hidden="true"></span>&nbsp;发布时间</label>
                                    <a href="#" id="post-date" data-type="datetime" data-pk="1" data-url="#" title="设置发布时间"><?=$post->post_date?></a>
                                    <input type="hidden" name="postDate" value="<?=$post->post_date?>" />
                                </li>
                            </ul>

                        </div>

                        <div class="panel-footer">
                            <a class="btn btn-sm btn-primary pull-right" href="javascript:void(0);" onclick="fun_publish_post();">立即发布</a>
                            <a class="btn btn-sm btn-default pull-left" href="javascript:;">
                                <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                                移动到回收站
                            </a>
                            <i class="clearfix"></i>
                        </div>
                    </div>

                    <div class="panel panel-default">
                        <div class="panel-heading">分类目录</div>
                        <div class="panel-body">
                            <ul class="list-unstyled post-category">
                                <?php foreach ( $category_arr as $category ) : ?>
                                    <li>
                                        <input type="checkbox" id="input-<?=$category->ID?>" name="categorys[]" <?php if ( in_array($category->ID,$post->category_ids) ) : echo 'checked'; endif;  ?>  value="<?=$category->ID?>">
                                        <label for="input-<?=$category->ID?>" class="icheck-text"><?=$category->category_name?></label>
                                    </li>
                                <?php endforeach; ?>
                            </ul>

                            <a href="#" class="btn btn-default btn-xs">+添加分类目录</a>
                        </div>
                    </div>

                    <div class="panel panel-default">
                        <div class="panel-heading">标签</div>
                        <div class="panel-body">
                            <select multiple class="select2" name="tags[]">
                                <?php if ( is_array($tag_arr) ) :  ?>
                                    <?php foreach ( $tag_arr as $tag ) : ?>
                                        <option value="<?=$tag->ID?>" <?php if ( in_array($tag->ID,$post->tag_ids) ): echo 'selected'; endif; ?> ><?=$tag->tag_name?></option>
                                    <?php endforeach; endif; ?>
                            </select>
                        </div>
                    </div>

                    <div class="panel panel-default">
                        <div class="panel-heading">封面图片</div>
                        <div class="panel-body">
                            <img id="guid-img" src="<?=$post->guid?>" />
                            <input type="hidden" id="guid" name="guid" value="<?=$post->guid?>" />
                            <a class="btn btn-xs btn-default center" href="javascript:fun_open_file_win();">
                                <span class="glyphicon glyphicon-picture" aria-hidden="true"></span>
                                选择一个特色图片
                            </a>
                            <a class="btn btn-xs btn-default" id="remove-guid" href="javascript:fun_remove_guid();">
                                取消选择
                            </a>
                        </div>
                    </div>

                </div>
            </div>

        </form>
    </div>

</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="<?=base_url();?>public/resource/default/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="<?=base_url();?>public/resource/default/vendor/datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script src="<?=base_url();?>public/resource/default/vendor/datetimepicker/locales/bootstrap-datetimepicker.zh-CN.js"></script>
<!-- Include CKEditor -->
<script src="<?=base_url();?>public/resource/default/vendor/ckeditor/ckeditor.js"></script>
<!-- Include ICheck -->
<script src="<?=base_url();?>public/resource/default/vendor/icheck/icheck.js"></script>
<!-- Include Select2 -->
<script src="<?=base_url();?>public/resource/default/vendor/select2/select2.min.js"></script>
<!-- Include Editable -->
<script src="<?=base_url();?>public/resource/default/vendor/bs3editable/js/bootstrap-editable.min.js"></script>

<script src="<?=base_url();?>public/resource/default/js/init.js"></script>
<script src="<?=base_url();?>public/resource/default/js/ck.js"></script>

<script>

    $(document).ready(function(){
        $('.post-category input').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue',
            increaseArea: '20%' // optional
        });

        $('.select2').select2({
            placeholder: "选择标签"
        });


        $('#post-status').editable({
            source: [
                {value: 'publish', text: '已发布'},
                {value: 'draft', text: '草稿'},
                {value: 'verify', text: '等待复审'}
            ],
            success: function(response,newValue)
            {
                $('input[name="postStatus"]').val(newValue);
            }
        });

        $('#ping-status').editable({
            source: [
                {value: 'public', text: '公开'},
                {value: 'private', text: '私密'},
                {value: 'password', text: '密码保护'}
            ],
            success: function(response,newValue) {
                $('input[name="pingStatus"]').val(newValue);
                if (newValue == 'password')
                {
                    $('input[name="postPassword"]').removeClass('hidden');
                }
                else
                {
                    $('input[name="postPassword"]').addClass('hidden');
                }
            }
        });

        $('#post-date').editable({
            placement: 'bottom',
            format: 'yyyy-mm-dd hh:ii:ss',
            viewformat: 'yyyy-mm-dd hh:ii:ss',
            datetimepicker: {
                weekStart: 1,
                language: 'zh-CN'
            },
            success: function(response,newValue) {
                $('input[name="postDate"]').val(newValue);
            }
        })

    });

    // initialize ckeditor
    init_ckeditor();

    function fun_publish_post() {

        var content = CKEDITOR.instances.editor.getData()
        $('input[name="postContent"]').val(content);

        $.ajax({
            url:       '<?=site_url(array("post","post_edit"))?>',
            type:      'post',
            dataType:  'json',
            data:      $('#new-post-fm').serialize(),
            async:     false,
            success: function(response){

                alert(response.resultMsg);

                if (response.resultCode == CHttp.getConstant('HTTP_STATUS_SUCCESS'))
                {
                    window.location.href = '<?=site_url(array("post","index"));?>';
                }
            },
            error: function(request){
                //alert('Error');
            }
        })
    }



    function fun_open_file_win() {

        var url = '<?=site_url(array("media","index"))?>';
        var name= '媒体库';
        var iWidth=810;
        var iHeight=600;
        //获得窗口的垂直位置
        var iTop = (window.screen.availHeight-30-iHeight)/2;
        //获得窗口的水平位置
        var iLeft = (window.screen.availWidth-10-iWidth)/2;
        var params='width='+iWidth
                +',height='+iHeight
                +',top='+iTop
                +',left='+iLeft
                +',channelmode=yes'//是否使用剧院模式显示窗口。默认为 no
                +',directories=no'//是否添加目录按钮。默认为 yes
                +',fullscreen=no' //是否使用全屏模式显示浏览器
                +',location=no'//是否显示地址字段。默认是 yes
                +',menubar=no'//是否显示菜单栏。默认是 yes
                +',resizable=no'//窗口是否可调节尺寸。默认是 yes
                +',scrollbars=no'//是否显示滚动条。默认是 yes
                +',status=bo'//是否添加状态栏。默认是 yes
                +',titlebar=no'//默认是 yes
                +',toolbar=no'//默认是 yes
            ;
        window.open( url, name, params );
    }

    /**
     * 图片选择窗口的回调
     * @param params
     */
    function fun_img_win_callback(params) {
        if (params)
        {
            $('#guid-img').prop('src',params);
            $('#guid').val(params);
        }
        return;
    }

    /**
     * 取消缩略图
     */
    function fun_remove_guid() {
        $('#guid-img').prop('src','');
        $('#guid').val('');
    }

</script>

</body>
</html>